<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible" class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" :rules="rules">
					<el-col :span="24">
						<el-form-item label="打印事件编号" prop="eventCode">
							<el-input v-model='dataForm.eventCode' placeholder='请输入' maxlength="50" show-word-limit  clearable :style='{"width":"100%"}' v-bind:disabled="disabled">
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<!-- <el-form-item label="打印配置Id" prop="printConfigId">
							<el-select v-model='dataForm.printConfigId' placeholder='请选择' clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in printConfigIdOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item> -->
                        <el-form-item label="打印配置名称" prop="printConfigId">
                        <SHZY-select
                            v-model="dataForm.printConfigId"
                            :list="printOptions"
                            label="name"
                            value="id"
                            placeholder="打印配置名称"
                        ></SHZY-select>
                    </el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="有效标志" prop="enabledMark">
							<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0' >
							</el-switch>
						</el-form-item>
					</el-col>
				</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import ApiArt from "@/api/common/event";
    import { getPrintConfigList } from "@/api/common/printConfig";
	export default {
		components: {  },
		props: [],
		data() {
			const validatePass = (rule, value, callback) => {
            if (!value || value.trim().length == 0) {
                callback(new Error("不能为空"));
            } else if (value.trim().length > 50) {
                callback(new Error("长度不能超过50字"));
            } else {
                callback();
            }
        };
			return {
				disabled:false,
                printOptions: [],
				btnLoading:false,
				loading: false,
				visible: false,
				dataForm: {
					id:'',
					eventCode:undefined,
					printConfigId:undefined,
					enabledMark:1,
				},
				rules: {
					eventCode: [
                    {
                        required: true,
                        message: "请输入打印事件编号",
                        validator: validatePass,
                        trigger: '"blur"'
                    }
                ],
				printConfigId: [
                    {
                        required: true,
                        message: "请选择打印事件名称",
                        validator: validatePass,
                        trigger: '"blur"'
                    }
                ],
				},
				//printConfigIdOptions:[{'fullName':"选项一",'id':"1"},{'fullName':"选项二",'id':"2"}],
			}
		},
		computed: {},
        watch: {},
        created() {
             this.getPrintList();
		},
		mounted() {
        },
		methods: {
             getPrintList() {
            getPrintConfigList().then(res => {
                this.printOptions = res.data.list;
            });
        },
			goBack() {
                this.$emit('refresh')
            },
			init(id) {
				this.dataForm.id = id || 0;
                this.visible = true;
				this.disabled=false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						this.disabled=true;
						ApiArt.getInfo(this.dataForm.id).then(res =>{
							this.dataForm = res.data;
						})
					}
				});
				this.$store.commit('generator/UPDATE_RELATION_DATA', {})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;
					const formMethod = !this.dataForm.id ? ApiArt.create : ApiArt.update;
					formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
				}
			})
			},
			// dataFormSubmit() {
			// 	this.$refs['elForm'].validate((valid) => {
            //         if (valid) {
			// 			this.btnLoading = true;
			// 			if (!this.dataForm.id) {
            //                 ApiArt.create(this.dataForm).then((res) => {
			// 					this.btnLoading = false;
            //                     this.$message({
            //                         message: res.msg,
            //                         type: 'success',
            //                         duration: 1000,
            //                         onClose: () => {
			// 							this.btnLoading = false;
            //                             this.visible = false,
            //                             this.$emit('refresh', true)
            //                         }
            //                     })
            //                 })
            //             } else {
            //                 ApiArt.update(this.dataForm).then((res) => {
			// 					this.btnLoading = false;
            //                     this.$message({
            //                         message: res.msg,
            //                         type: 'success',
            //                         duration: 1000,
            //                         onClose: () => {
			// 							this.btnLoading = false;
            //                             this.visible = false
            //                             this.$emit('refresh', true)
            //                         }
            //                     })
            //                 })
            //             }
            //         }
            //     })
			// },
		}
	}
</script>
